package day6

import org.apache.spark.sql.{DataFrame, SparkSession}

object Test2 {
  def main(args: Array[String]): Unit = {
    // 给数据添加表名
    val spark = SparkSession.builder().master("local[*]").appName("cheshi1").getOrCreate()
    val df: DataFrame = spark.read.json("file:///D:\\data\\a.json")
    df.createTempView("t_person")
    val df2 = spark.sql("select * from t_person where age > 20")
    // 方式二
    /*val df2 = spark.sql(
      """
        |
        | select name,age, case when age < 18 then '未成年'
        |                         when age < 60 then '成年'
        |                         else '老年'
        |                         end x1
        |                       from t_person
        |
        |""".stripMargin).show()*/

    df2.printSchema()
    println("-----------------------------------------------")
    df2.show()
  }
}
